package dataStructure.kmp;

/**
 * @author lhn
 * @date 2024-09-10 16:05
 */

public class ViolenceMatch {
    public static void main(String[] args) {
        String str1 = "硅硅谷 尚硅谷你尚硅 尚硅谷你尚硅谷你尚硅你好";
        String str2 = "尚硅谷你尚硅你";
        System.out.println(violenceMatch(str1, str2));
    }

    public static int violenceMatch(String str1, String str2) {
        char[] s1 = str1.toCharArray(); // 将字符串转换为字符数组
        char[] s2 = str2.toCharArray();

        int s1Len = s1.length;
        int s2Len = s2.length;

        int i = 0;  // 用于遍历s1
        int j = 0;  // 用于遍历s2

        while (i < s1Len && j < s2Len) {
            if (s1[i] == s2[j]) {
                i ++;
                j ++;
            } else {
                i = i - j + 1; // i回退到j的下一个位置
                j = 0;         // j重置为0
            }
        }

        if (j == s2Len) {
            return i - j;
        } else {
            return -1;
        }
    }
}
